<?php
/**
 * Product Management
 **/
class Admin_ProductcategoriesController extends Zend_Controller_Action{
    function init(){
        System_Template::using('getit','Admin');
        // get parameter in request URL
        $this->Params = $this->getRequest()->getParams();
        $this->view->maintitle = "Product Category";
        $this->model = new Admin_Model_Productcategories();
        parent::init();
    }
    
    function indexAction(){
        $storeId = $this->_request->getParam("STOREID");
        
        if(isset($this->Params['limit'])){
            $limit = (int) $this->Params['limit'];
        }else{
            $limit = 10;
        }
        
        if(isset($this->Params['page'])){
            $page = (int) $this->Params['page'];
        }else{
            $page = 1;
        }
        
        $numberOfItem = $this->model->count($storeId);
        $data = $this->model->fetchAll($storeId,$limit,($page-1)*$limit);
        // Doan nay dam bao phan trang dung khi xoa
        if($page > 1 && sizeof($data) == 0){
            $page = $page - 1;
            $data = $this->model->fetchAll($storeId,$limit,($page-1)*$limit);
        }
        $this->view->data = $data;
        $this->view->limit = $limit;
        $this->view->page = $page;
        $this->view->pagination = common::pagination("/admin/productcategory/index",$numberOfItem,'right', $limit, $page);
    }
    
    /**
     *
     * @author uyen.do
     * INVISIBLE
     * Redirect to add or edit form
     * 
     **/
    
    function inputAction(){
        $storeId = $this->_request->getParam("STOREID");
        $id = (isset($this->Params['id']) ? $this->Params['id'] : 0);
        if($id > 0){
            $data = $this->model->find($id)->current();
            $this->view->data = $data;
            $this->view->id = $id;
        }
    }
    
    /**
     *@author uyen.do
     * INVISIBLE
     * Save a new Product Category or edit an existed Product Category
     *
     **/
    
    function saveAction(){
        if($this->Params['PRODUCTCATEGORYID'] == ""){
            $this->model->addNewProductcategory($this->Params);
        }else{
            $this->model->updateProductcategory($this->Params);
        }
        
        // Luu lai phan trang truoc
        if(!issset($this->Params['limit'])){
            $this->_redirect('/admin/productcategory/index');
        }else{
            $this->_redirect('/admin/productcategory/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
        }
    }
    
    /**
     * @author uyen.do
     * INVISIBLE
     * Delete an Product Category
     *
     * */
    
    function deleteProduct(){
        $id = $this->Params['id'];
        $this->model->deleteProductcategory($id);
        // Luu lai phan trang truoc
        if(!issset($this->Params['limit'])){
            $this->_redirect('/admin/productcategory/index');
        }else{
            $this->_redirect('/admin/productcategory/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
        }
    }
    
    /**
     *
     * @author uyen.do
     * delete seleted Product Category
     *
     **/
    
    function deletemultiproductcategoryAction(){
        $ids = $this->Params['ITEM'];
        $this->logger->info(sizeof($ids));
        foreach( $ids as $id){
           $this->model->deleteProductcategory($id);
        }
        
        // Luu lai phan trang truoc
        if(!issset($this->Params['limit'])){
            $this->_redirect('/admin/productcategory/index');
        }else{
            $this->_redirect('/admin/productcategory/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
        }
    }
}
?>